package com.example.cxz2021606127.newtypefragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.cxz2021606127.bean.NewsInforBean;
import com.example.cxz2021606127.R;
import com.example.cxz2021606127.adapter.MyRecyclerViewAdapter;
import com.example.cxz2021606127.newtypefragment.NewDate;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class GuoNeiFragment extends Fragment {
    View view;
    private RecyclerView rv;
    private OkHttpClient client;
    private FloatingActionButton fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_guo_nei, container, false);
            fab=view.findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    rv.smoothScrollToPosition(0);
                }
            });

        }

        return view;

    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        rv = view.findViewById(R.id.rv);
        client = new OkHttpClient();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .addHeader("Authorization", "APPKEY" + NewDate.APPKEY)
                        .url(NewDate.NewsList_guonei)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                NewsInforBean newsInforBean = gson.fromJson(result, NewsInforBean.class);
                                List<NewsInforBean.ResultBean.ListBean> list = newsInforBean.getResult().getList();

                                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                                rv.setLayoutManager(layoutManager);

                                MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(list,getContext());
                                rv.setAdapter(adapter);

                            }
                        });
                    }
                });
            }
        }).start();
    }


}